#include "urt_driver.h"
#include <avr/interrupt.h>

/*********************/
/*	Local Dfinitions */
/*********************/


#define u8DATA_NOT_PRESENT			(u8)0

#define u8DATA_PRESENT_RAM			(u8)1

#define u8DATA_PRESENT_FLASH		(u8)2





/*******************/
/*	Local Variables*/
/*******************/

static u8 au8ReceiveBuffer[URT_nRECEIVE_BUFFER_LENGTH] = {0};

static BUF_tstrBuffer  strRceiveBufferControl;

static u8 * pku8TransmitData;

static u8 u8DataCounter;

static u8 * pu8TransmitRamData;

static u8 u8DataPresent;

static u8 u8TransmitIndex = TYP_u8ZERO;


/*********************************/
/*	Local Functions' Declaration */
/*********************************/




/***********************************/
/*	External Functions' Definition */
/***********************************/
void URT_vidInit(void)
{
    
    UCSRB = (1 << RXCIE) | (1 << TXCIE) | (1 << RXEN) | ( 1 << TXEN);
    
    /* Set baud rate to 9600 b/s*/
    UBRRL = (u8)12;

    
   BUF_vidInit(&strRceiveBufferControl, au8ReceiveBuffer,  (u16)URT_nRECEIVE_BUFFER_LENGTH) ;
    
      

    
    
    
   return;    
}

TYP_tu8Bool URT_bSendByte(u8 u8Data)
{
    TYP_tu8Bool bReturnValue;
    u8 u8TxcTemp;
    
    
    u8TxcTemp = ((UCSRA & (1<<TXC)) >> TXC);
    
    if (u8TxcTemp == TYP_u8ZERO)    
    {                
        UCSRA |= (1<<TXC);        
        UDR = u8Data;        
        bReturnValue = TYP_u8TRUE;    
    }    
    else    
    {        
        bReturnValue = TYP_u8FALSE;    
    }    
    
    return bReturnValue; 
    
    
}


TYP_tu8Bool URT_bSendString(u8  * pu8SendString )
{
    TYP_tu8Bool bReturnResult;
    u8          u8TempData;
            
    if (u8DataPresent == u8DATA_NOT_PRESENT)    
    {        
        u8TransmitIndex = TYP_u8ZERO;
		pku8TransmitData  =   pu8SendString;
        u8TempData = pgm_read_byte(&pku8TransmitData[u8TransmitIndex]);
        URT_bSendByte(u8TempData);
        u8TransmitIndex++;       
        u8DataPresent    =   u8DATA_PRESENT_FLASH;        
        bReturnResult   =   TYP_u8TRUE;    
        
    }    
    else    
    {        
        bReturnResult   =   TYP_u8FALSE;    
    }        
    
    return bReturnResult; 
   
}


TYP_tu8Bool URT_bSendRAMString(u8 * pu8SendString)
{
    TYP_tu8Bool bReturnResult;
    u8          u8TempData;
            
    if (u8DataPresent == u8DATA_NOT_PRESENT)    
    {    
		pu8TransmitRamData  =   pu8SendString;
		u8TransmitIndex = TYP_u8ZERO;    
        u8TempData = pu8TransmitRamData[u8TransmitIndex];
        URT_bSendByte(u8TempData);
        u8TransmitIndex++;       
        u8DataPresent    =   u8DATA_PRESENT_RAM;        
        bReturnResult   =   TYP_u8TRUE;    
        
    }    
    else    
    {        
        bReturnResult   =   TYP_u8FALSE;    
    }        
    
    return bReturnResult; 
   
}

BUF_tstrBuffer * URT_strGetReceiveBuffer(void)
{
    return &strRceiveBufferControl;
}


ISR(USART_TXC_vect)
{   
    u8          u8TempData;    
    if ( u8DataPresent == u8DATA_PRESENT_FLASH)    
    {        
        
        u8TempData = pgm_read_byte(&pku8TransmitData[u8TransmitIndex]);
        if ( u8TempData != '\0' )        
        {            
            URT_bSendByte(u8TempData);            
			u8TransmitIndex++;
            u8DataCounter++;                 
            if (u8DataCounter == (u8)255)                 
            {                    
                u8DataPresent = TYP_u8FALSE;                    
                u8DataCounter = TYP_u8ZERO;                 
            }            
                    
        }        
        else        
        {            
            u8DataCounter = TYP_u8ZERO;            
            u8DataPresent = TYP_u8FALSE;        
        }
    }
	else if (u8DataPresent == u8DATA_PRESENT_RAM)
	{
		u8TempData = pu8TransmitRamData[u8TransmitIndex];
        if ( u8TempData != '\0' )        
        {            
            URT_bSendByte(u8TempData);            
            u8DataCounter++;                 
            if (u8DataCounter == (u8)255)                 
            {                    
                u8DataPresent = u8DATA_NOT_PRESENT;                    
                u8DataCounter = TYP_u8ZERO;                 
            }            
                    
        }
	}
    
   return; 
}



ISR(USART_RXC_vect)
{
    u8 u8TempData;
    u8 u8ReturnValue;                
          
    u8TempData = UDR;

    u8ReturnValue = BUF_u8AddToEnd(&strRceiveBufferControl, u8TempData);      
    if( u8ReturnValue == TYP_u8FALSE )                
    {                        
        BUF_vidFlush(&strRceiveBufferControl);                
    }        
   return;    
}



/********************************/
/*	Local Functions' Definition */
/********************************/
